package flc.ast.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import flc.ast.R;
import flc.ast.activity.AnalogCameraActivity;
import flc.ast.activity.ComicCameraActivity;
import flc.ast.activity.ImgDetailActivity;
import flc.ast.activity.SelPicActivity;
import flc.ast.adapter.MyWorksAdapter;
import flc.ast.adapter.RecordAdapter;
import flc.ast.databinding.FragmentHomeBinding;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.RxUtil;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends BaseNoModelFragment<FragmentHomeBinding> {

    private RecordAdapter recordAdapter;
    private List<String> listShow = new ArrayList<>();

    @Override
    protected int onCreate() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.ivComicCamera.setOnClickListener(this);
        mDataBinding.ivAnalogCamera.setOnClickListener(this);
        mDataBinding.ivPicRepair.setOnClickListener(this);
        mDataBinding.ivPicEnhance.setOnClickListener(this);
        mDataBinding.ivPicStyle.setOnClickListener(this);
        mDataBinding.ivAestheticComic.setOnClickListener(this);
        mDataBinding.ivAmericanComic.setOnClickListener(this);

        mDataBinding.rvRecordList.setLayoutManager(new GridLayoutManager(mContext, 3));
        recordAdapter = new RecordAdapter();
        mDataBinding.rvRecordList.setAdapter(recordAdapter);
        recordAdapter.addChildClickViewIds(R.id.ivRecordItemDelete);
        recordAdapter.setOnItemChildClickListener(this);
        recordAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivComicCamera://漫画相机
                startActivity(ComicCameraActivity.class);
                break;
            case R.id.ivAnalogCamera://模拟相机
                startActivity(AnalogCameraActivity.class);
                break;
            case R.id.ivPicRepair://修复老照片
                SelPicActivity.type = 0;
                startActivity(SelPicActivity.class);
                break;
            case R.id.ivPicEnhance://图像增强
                SelPicActivity.type = 1;
                startActivity(SelPicActivity.class);
                break;
            case R.id.ivPicStyle://图像风格
                SelPicActivity.type = 2;
                startActivity(SelPicActivity.class);
                break;
            case R.id.ivAestheticComic://唯美漫画
                SelPicActivity.type = 3;
                startActivity(SelPicActivity.class);
                break;
            case R.id.ivAmericanComic://美式漫画
                SelPicActivity.type = 4;
                startActivity(SelPicActivity.class);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        ImgDetailActivity.isLocal = false;
        ImgDetailActivity.path = recordAdapter.getItem(position);
        startActivity(ImgDetailActivity.class);
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        switch (view.getId()) {
            case R.id.ivRecordItemDelete:
                delete(recordAdapter.getItem(position));
                break;
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        getData();
    }

    private void getData() {
        listShow.clear();
        //获取文件夹下文件
        String path = PathUtils.getFilesPathExternalFirst() + Content.myRecord;
        List<File> files = FileUtils.listFilesInDir(path);
        if (files.size() > 0) {
            for (File file : files) {
                listShow.add(file.getPath());
            }
        }
        Collections.sort(listShow, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                long t1 = FileUtils.getFileLastModified(o1);
                long t2 = FileUtils.getFileLastModified(o2);
                return t2 > t1 ? 1 : -1;
            }
        });
        if (listShow.size() > 0) {
            recordAdapter.setList(listShow);
            mDataBinding.rvRecordList.setVisibility(View.VISIBLE);
        } else {
            mDataBinding.rvRecordList.setVisibility(View.GONE);
        }
    }

    //删除
    private void delete(String path) {
        showDialog(getString(R.string.delete_ing));
        RxUtil.create(new RxUtil.Callback<String>() {

            @Override
            public void doBackground(ObservableEmitter<String> emitter) {
                FileUtils.delete(path);
                emitter.onNext("");
            }

            @Override
            public void accept(String path) {
                dismissDialog();
                ToastUtils.showShort(getString(R.string.delete_suc));
                getData();
            }
        });
    }

}